package com.ruoyi.lowcode.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lowcode.domain.ApiMapping;
import com.ruoyi.lowcode.service.IApiMappingService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 路由管理Controller
 * 
 * @author ruoyi
 * @date 2023-07-03
 */
@Controller
@RequestMapping("/lowcode/apimapping")
public class ApiMappingController extends BaseController
{
    private String prefix = "lowcode/apimapping";

    @Autowired
    private IApiMappingService apiMappingService;

    @RequiresPermissions("lowcode:apimapping:view")
    @GetMapping()
    public String apimapping()
    {
        return prefix + "/apimapping";
    }

    /**
     * 查询路由管理列表
     */
    @RequiresPermissions("lowcode:apimapping:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ApiMapping apiMapping)
    {
        startPage();
        List<ApiMapping> list = apiMappingService.selectApiMappingList(apiMapping);
        return getDataTable(list);
    }

    /**
     * 导出路由管理列表
     */
    @RequiresPermissions("lowcode:apimapping:export")
    @Log(title = "路由管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ApiMapping apiMapping)
    {
        List<ApiMapping> list = apiMappingService.selectApiMappingList(apiMapping);
        ExcelUtil<ApiMapping> util = new ExcelUtil<ApiMapping>(ApiMapping.class);
        return util.exportExcel(list, "路由管理数据");
    }

    /**
     * 新增路由管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存路由管理
     */
    @RequiresPermissions("lowcode:apimapping:add")
    @Log(title = "路由管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ApiMapping apiMapping)
    {
        return toAjax(apiMappingService.insertApiMapping(apiMapping));
    }

    /**
     * 修改路由管理
     */
    @RequiresPermissions("lowcode:apimapping:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ApiMapping apiMapping = apiMappingService.selectApiMappingById(id);
        mmap.put("apiMapping", apiMapping);
        return prefix + "/edit";
    }

    /**
     * 修改保存路由管理
     */
    @RequiresPermissions("lowcode:apimapping:edit")
    @Log(title = "路由管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ApiMapping apiMapping) throws Exception {
        return toAjax(apiMappingService.updateApiMapping(apiMapping));
    }

    /**
     * 删除路由管理
     */
    @RequiresPermissions("lowcode:apimapping:remove")
    @Log(title = "路由管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(apiMappingService.deleteApiMappingByIds(ids));
    }
}
